use actix_web::{delete, get, post, put, HttpResponse};
use actix_web::web::{Data, Json, Path, Query, ReqData};
use crate::base::DbPool;
use crate::base::errors::AppError;
use crate::base::response::{ApiResponse, FateResult};
use crate::data::dto::user_dto::{CreateUserRequest, ModifyStatusRequest, UserInfoResponse, UserPageRequest};
use crate::data::model::user::User;
use crate::service::user_service::UserService;


const USER_TAG: &str = "用户管理";

/// 新增用户
/// 
/// 创建系统用户
#[utoipa::path(
    tag = USER_TAG,
    post,
    path = "/api/user/add",
    request_body = CreateUserRequest,
    responses(
        (status = 200, description = "操作成功", body = ApiResponse<User>),
        (status = 400, description = "请求数据验证失败", body = String),
        (status = 500, description = "服务器内部错误", body = String)
    )
)]
#[post("/add")]
pub async fn user_add(pool: Data<DbPool>, user_id: ReqData<i64>, request: Json<CreateUserRequest>) 
-> Result<HttpResponse, AppError> {
    let response = UserService::add_user(&pool, &user_id, request.into_inner()).await?;
    Ok(ApiResponse::success(response))
}

/// 获取用户详情
/// 
/// 根据ID获取用户详情信息，包含权限与菜单
#[utoipa::path(
    tag = USER_TAG,
    get,
    path = "/api/user/info/{id}",
    params(
        ("id" = i64, description = "用户ID")
    ),
    responses(
        (status = 200, description = "操作成功", body = ApiResponse<UserInfoResponse>),
        (status = 400, description = "请求数据验证失败", body = String),
        (status = 500, description = "服务器内部错误", body = String)
    )
)]
#[get("/info/{id}")]
pub async fn user_info(db_pool: Data<DbPool>, id: Path<i64>) -> FateResult {
    let response = UserService::info(&db_pool, &id).await?;
    Ok(ApiResponse::success(response))
}

/// 获取用户分页
/// 
/// 获取用户分页信息
#[utoipa::path(
    tag = USER_TAG,
    get,
    path = "/api/user/page",
    params(
        UserPageRequest
    ),
    responses(
        (status = 200, description = "操作成功", body = ApiResponse<User>),
        (status = 400, description = "请求数据验证失败", body = String),
        (status = 500, description = "服务器内部错误", body = String)
    )
)]
#[get("/page")]
pub async fn user_page(db_pool: Data<DbPool>, params: Query<UserPageRequest>) -> FateResult {
    let response = UserService::page(&db_pool, params.into_inner()).await?;
    Ok(ApiResponse::success(response))
}

/// 修改用户状态
/// 
/// 修改用户的状态信息
#[utoipa::path(
    tag = USER_TAG,
    put,
    path = "/api/user/modify_status/{id}",
    params(
        ("id" = i64, description = "用户ID"),
    ),
    request_body = ModifyStatusRequest,
    responses(
        (status = 200, description = "操作成功", body = ApiResponse<User>),
        (status = 400, description = "请求数据验证失败", body = String),
        (status = 500, description = "服务器内部错误", body = String)
    )
)]
#[put("/modify_status/{id}")]
pub async fn user_modify_status(db_pool: Data<DbPool>, id: Path<i64>, params: Json<ModifyStatusRequest>) -> FateResult {
    if params.id != *id {
        return Ok(ApiResponse::error("用户ID校验失败，请检查输入参数".to_string()))
    }
    Ok(ApiResponse::success(UserService::modify_status(&db_pool, &params).await?))
}

/// 删除用户
/// 
/// 根据用户ID删除用户
#[utoipa::path(
    tag = USER_TAG,
    delete,
    path = "/api/user/delete/{id}",
    params(
        ("id" = i64, description = "用户ID")
    )
)]
#[delete("/delete/{id}")]
pub async fn user_delete(db_pool: Data<DbPool>, id: Path<i64>) -> FateResult {
    Ok(ApiResponse::success(UserService::delete(&db_pool, &id).await?))
}